/**************************************************************

Copyright Astrium Services GEO-Information / Spot Image
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

 ***************************************************************/
package com.spotimage.catalog.client.ui.tab;

import java.util.Date;

import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.SliderItem;
import com.smartgwt.client.widgets.form.fields.SpinnerItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.spotimage.catalog.client.ui.MapPanel;
import com.spotimage.catalog.client.ui.SearchButton;
import com.spotimage.catalog.client.ui.Ui;
import com.spotimage.catalog.client.ui.component.MyDateItem;
import com.spotimage.catalog.client.ui.component.MySliderItem;
import com.spotimage.catalog.client.ui.component.ResolutionItem;

/**
 * Search tab
 */
public class SearchTab extends Tab {

	private static final String TITLE = "Search";
	private DynamicForm parametersForm;
	private DateItem startDate;
	private DateItem endDate;
	private DynamicForm advancedParametersForm;
	private SliderItem maxCloud;
	private ResolutionItem maxResolution;
	private ResolutionItem minResolution;
	private SpinnerItem maxIncidence;

	/**
	 * Build UI.
	 */
	public SearchTab(MapPanel mapPanel) {
		super(TITLE, "[SKIN]/actions/search.png");
		
		VLayout layout = new VLayout(5);
		layout.setPadding(new Integer(5));
		layout.setBackgroundColor(Ui.BACKGROUND_COLOR);
		layout.setBorder("1px solid " + Ui.BORDER_COLOR);

		final SectionStack sectionStack = new SectionStack();
		sectionStack.setVisibilityMode(VisibilityMode.MULTIPLE);
		sectionStack.setWidth100();
		sectionStack.setHeight100();
		
		SectionStackSection parameters = new SectionStackSection("Parameters");
		parameters.setExpanded(true);
		parameters.setCanCollapse(false);
		parameters.addItem(setParametersForm());
		sectionStack.addSection(parameters);

		SectionStackSection advancedParameters = new SectionStackSection("Advanced parameters");
		advancedParameters.setExpanded(false);
		advancedParameters.addItem(setAdvancedParametersForm());
		sectionStack.addSection(advancedParameters);
		
		layout.addMember(sectionStack);
		
		HLayout buttonLayout = new HLayout();
		buttonLayout.addMember(new SearchButton(this, mapPanel));
		layout.addMember(buttonLayout);

		setPane(layout);
	}

	/**
	 * Build parameters form UI.
	 * @return 
	 */
	@SuppressWarnings("deprecation")
	private DynamicForm setParametersForm() {
		parametersForm = new DynamicForm();
		parametersForm.setWidth100();
		
		Date date = new Date();
		
		endDate = new MyDateItem("End date", date);
		
		date.setYear(date.getYear() - 1);
		startDate = new MyDateItem("Start date", date);
		
		startDate.setDefaultValue(date);
		startDate.setRequired(true);
		
		maxCloud = new MySliderItem("Maximum cloud %");
		maxCloud.setWrapTitle(false);
		maxCloud.setMinValue(0);
		maxCloud.setMaxValue(100);
		maxCloud.setDefaultValue(20);
		
		parametersForm.setFields(startDate, endDate, maxCloud);
		
		return parametersForm;
	}

	/**
	 * Build advanced parameters form UI.
	 * @return
	 */
	private DynamicForm setAdvancedParametersForm() {
		advancedParametersForm = new DynamicForm();
		advancedParametersForm.setWidth100();
		
		maxResolution = new ResolutionItem("Maximum resolution");
		maxResolution.setDefaultValue(ResolutionItem.MIN_VALUE);
		
		minResolution = new ResolutionItem("Minimum resolution");
		minResolution.setDefaultValue(ResolutionItem.MIN_VALUE);

		maxIncidence = new SpinnerItem();
		maxIncidence.setWrapTitle(false);
		maxIncidence.setTitle("Maximum incidence");
		maxIncidence.setDefaultValue(30);
		maxIncidence.setMin(0);
		maxIncidence.setMax(30);
		
		advancedParametersForm.setFields(maxResolution, minResolution, maxIncidence);
		
		return advancedParametersForm;
	}

	/**
	 * @return 
	 */
	public DynamicForm getParametersForm() {
		return parametersForm;
	}

	/**
	 * @return 
	 */
	public DynamicForm getAdvancedParametersForm() {
		return advancedParametersForm;
	}

	/**
	 * @return 
	 */
	public DateItem getStartDate() {
		return startDate;
	}

	/**
	 * @return 
	 */
	public DateItem getEndDate() {
		return endDate;
	}

	/**
	 * @return 
	 */
	public SliderItem getMaxCloud() {
		return maxCloud;
	}

	/**
	 * @return 
	 */
	public SpinnerItem getMaxIncidence() {
		return maxIncidence;
	}

	/**
	 * @return 
	 */
	public ResolutionItem getMinResolution() {
		return minResolution;
	}

	/**
	 * @return 
	 */
	public ResolutionItem getMaxResolution() {
		return maxResolution;
	}

}
